package com.example.toolproject.demo.controller;

import com.example.toolproject.demo.domain.ScenicArea;
import com.example.toolproject.demo.domain.ScenicSport;
import com.example.toolproject.demo.domain.User;
import com.example.toolproject.demo.service.ScenicAreaService;
import com.example.toolproject.demo.service.ScenicSportService;
import com.example.toolproject.demo.util.FileUploadAndDownload;
import com.example.toolproject.demo.util.ResultBean;
import com.example.toolproject.demo.util.jwt.JwtUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ScenicAreaController
 * @Description TODO
 * @Author lvyongwei
 * @Date 2020-06-19 10:47
 **/
@RestController
@CrossOrigin
@RequestMapping("/scenicArea")
public class ScenicAreaController {

    @Autowired
    private ScenicAreaService scenicAreaService;

    @Autowired
    private ScenicSportService scenicSportService;

    /**
     * 獲取所有景區景點信息
     * @return
     */
    @GetMapping("/getAllScenicArea")
    public ResultBean getAllScenicArea(Integer pageNum, Integer pageSize, HttpServletRequest request){
        //获取前端传来的token
        String token = request.getHeader("Authorization");
        if(token==null || token.equals("")){
            return ResultBean.error("无token,请先登录");
        }
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        //判断token是否失效
        if(u == null){
            return ResultBean.error("token无效");
        }
        /**
         * 获取当前请求的用户id
         */
        int userId=u.getId();
        //给页码赋初值
        if(pageNum==null || pageNum==0){
            pageNum=1;
        }
        if(pageSize==null || pageSize==0){
            pageSize=10;
        }
        //执行分页
        PageHelper.startPage(pageNum,pageSize);
        //获取所有景區信息
        List<ScenicArea> list = scenicAreaService.getAllScenicArea();
        //获取分页信息
        PageInfo<ScenicArea> pageInfo=new PageInfo<ScenicArea>(list);
        //添加景点信息
        for(ScenicArea scenicArea:pageInfo.getList()){
            scenicArea.setScenicSport(scenicSportService.getAllScenicSport(scenicArea.getId(),userId));
        }
        return ResultBean.success(pageInfo);
    }


    /**
     * 根据输入信息进行模糊查询
     * @return
     */
    @GetMapping("/getScenicAreaBySearchMessage")
    public ResultBean getScenicAreaBySearchMessage(Integer pageNum, Integer pageSize,String searchMessage, HttpServletRequest request){
        //获取前端传来的token
        String token = request.getHeader("Authorization");
        if(token==null || token.equals("")){
            return ResultBean.error("无token,请先登录");
        }
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        //判断token是否失效
        if(u == null){
            return ResultBean.error("token无效");
        }
        //给页码赋初值
        if(pageNum==null || pageNum==0){
            pageNum=1;
        }
        if(pageSize==null || pageSize==0){
            pageSize=10;
        }
        /*//判断查询条件是否为空
        if(StringUtils.isEmpty(searchMessage) || searchMessage==null){
            return ResultBean.error("请输入查询信息");
        }*/
        int userId=u.getId();
        //执行分页
        PageHelper.startPage(pageNum,pageSize);
        //根据搜索信息对景区信息模糊查询
        List<ScenicArea> scenicAreaList=scenicAreaService.getScenicAreaBySearchMessage(searchMessage);
        //获取分页信息
        PageInfo<ScenicArea> pageInfo=new PageInfo<ScenicArea>(scenicAreaList);
        //添加景点信息
        for(ScenicArea scenicArea:pageInfo.getList()){
            scenicArea.setScenicSport(scenicSportService.getAllScenicSport(scenicArea.getId(),userId));
        }
        if(pageInfo.getList().size()==0){
            return  ResultBean.error("未查询到任何数据");
        }
        return ResultBean.success("成功获取数据信息",pageInfo);
    }

    @Autowired
    private FileUploadAndDownload fileUploadAndDownload;

    /**
     * 添加景点信息
     * @param file
     * @param scenicArea
     * @return
     */
    @PostMapping("/insertScenicArea")
    @ResponseBody
    public ResultBean insertScenicArea(@RequestParam("file") MultipartFile file,ScenicArea scenicArea, HttpServletRequest request){
        //获取前端传来的token
        String token = request.getHeader("Authorization");
        if(token==null || token.equals("")){
            return ResultBean.error("无token,请先登录");
        }
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        //判断token是否失效
        if(u == null){
            return ResultBean.error("token无效");
        }
        String fileName="";
        if(file.getSize()==0){
            return ResultBean.error("请上传文件");
        }else{
            /**
             * 文件上传
             */
            fileName=fileUploadAndDownload.upload(file,"\\scenicArea");
        }
        scenicArea.setScenicImage("\\scenicArea\\"+fileName);
        try{
            scenicAreaService.insertScenicArea(scenicArea);
            return ResultBean.success("添加景點信息成功");
        }catch (Exception e){
            return ResultBean.error("添加景點信息失敗");
        }
    }
}
